//
//  YZSecurityVM.m
//  VET
//
//  Created by Lam BG on 2018/7/6.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZSecurityVM.h"

#import "YZUser.h"

#import "YZSignAPI.h"

#import "NSString+Predicate.h"

@implementation YZSecurityVM

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setup];
    }
    
    return self;
}

- (void)setup {
    
    @weakify(self)
    // 手机号码
    [[RACObserve(self, number) distinctUntilChanged] subscribeNext:^(NSString *number) {
        
        @strongify(self)
        if ([number isValidMobileNumber]) {
            
            // 发起验证号码请求
            [[YZSignAPI apiCheckUserMobile:number] subscribeNext:^(id  _Nullable x) {
                
                BOOL isExist = [x boolValue];
                if (isExist) {
                    
                    // 号码存在被注册过
                    self.numberWarningInfo = @"该手机号码已注册";
                } else {
                    
                    // 号码不存在 可以注册
                }
                self.canVerCode = !isExist;
                
            } error:^(NSError * _Nullable error) {
                
                self.numberWarningInfo = @"请输入正确的手机号码";
                
            } completed:^{
                
                NSLog(@"completed");
            }];
        } else {
            
            // 当输入到达 11 位后 不是号码
            if (number.length == 11) {
                
                self.numberWarningInfo = @"请输入正确的手机号码";
                
            } else {
                
                self.numberWarningInfo = @"";
            }
            
            self.canVerCode = NO;
        }
    }];
    
    
    
    // 验证码
    _verCodeCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            @strongify(self)
            [[YZSignAPI apiGetUserMessage:self.number] subscribeNext:^(id  _Nullable x) {
                
                [subscriber sendNext:x];
                [subscriber sendCompleted];
            } error:^(NSError * _Nullable error) {
                
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{}];
        }];
    }];
    [_verCodeCmd.executing subscribeNext:^(id executing) {
        
        @strongify(self)
        BOOL isExecuting = [executing boolValue];
        self.canVerCode = !isExecuting;
    }];
    
    
    
    // 注册按钮是否可用
    _bindSignal = [RACSignal combineLatest:@[RACObserve(self, canVerCode), RACObserve(self, verCode), RACObserve(self, number)] reduce:^id(id canVerCode, NSString *verCode, NSString *number){
        
        return @([canVerCode boolValue] && verCode.length && number.length);
    }];
    _bindCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            @strongify(self)
            [[YZSignAPI apiLoginUserMobile:@"15817183527" andPassword:@"123456" deviceToken:[YZUser sharedInstance].deviceToken] subscribeNext:^(NSDictionary *data) {
                
                // 保存用户数据
                NSLog(@"%@", data);
                [[YZUser sharedInstance] storeUserToLocal:data];
                [subscriber sendNext:data];
                
            } error:^(NSError * _Nullable error) {
                
                NSLog(@"NSError: %@", error);
                [subscriber sendError:error];
                
            } completed:^{
                
                NSLog(@"completed");
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{}];
        }];
    }];
}

@end













